<?php

class UserMapper extends AMapper
{
	protected static $self = NULL;
	public static function &get() { return parent::_get(__CLASS__); }
	protected static function &getSelf() { return self::$self; }
	protected static function setSelf($in_self) { self::$self = $in_self; }

	public function find($in_user_id)
	{
		$userdata = DBConn::selectRow(sprintf("SELECT * FROM users WHERE id=%d;",$in_user_id));
		if(!$userdata || empty($userdata))
		{
			throw new FrameworkRecordNotFoundException("user");
		}
		return $this->load($userdata);
	}
	
	public function findByUsernameAndPassword($in_username, $in_password)
	{
		$userdata = DBConn::selectRow(sprintf("SELECT * FROM users WHERE name=%s AND password=%s", DBConn::quote($in_username), DBConn::quote(sha1($in_password))));
		if(!$userdata || empty($userdata))
		{
			throw new FrameworkRecordNotFoundException("user");
		}
		return $this->load($userdata);
	}

	public function insert($in_user)
	{
		return $this->automaticInsert($in_user);
	}

	public function update($in_user)
	{
		return $this->automaticUpdate($in_user);
	}

	public function &load($in_user_array)
	{
		$entity = substr(__CLASS__,0,-6);
		$user = new $entity($in_user_array);
		return $user;
	}
}

?>
